package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ScientificMapper;
import com.translate.entity.Scientific;
import com.translate.vo.contract.IScientificVO;
import com.translate.web.service.IScientificService;
import com.common.util.CopyEntity;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ScientificServiceImpl extends BaseTranslateServiceImpl<Scientific> implements IScientificService {
	
	IOSSConfig config;
	
	
	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}

	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<Scientific>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ScientificServiceImpl(ScientificMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(Scientific data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(Scientific data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<Scientific> getEntityClass() {
		return Scientific.class;
	}

	@Override
	public <VO extends IScientificVO> List<VO> getListFromCache(Class<VO> target) {
		List<Scientific> listToCache = this.listToCache(RedisConstant.SCIENTIFIC_KEY, 1);
		
		if (0 == listToCache.size()) {
			return new ArrayList<>();
		}
		
		String url = config.getURL();
		
		for (Scientific scientific : listToCache) {
            scientific.setBannerPic(url.concat(scientific.getBannerPic()));
        }
		
		return this.entityConvertToVo(listToCache, target);

	}

	@Override
	public <VO extends IScientificVO> VO getArticleById(int id, Class<VO> target) {
		Scientific articleDetailToCache = this.getArticleDetailToCache(id, redisTemplate,RedisConstant.getActivityId(id));
		
		if (null == articleDetailToCache) {
			return null;
		}
		
		return CopyEntity.copyEntity(articleDetailToCache, target);
	}
}